package fleditor.common
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class DialogBoxManager
	{
		private var _stage:Stage;
		private var _background:Sprite;
		
		public function DialogBoxManager()
		{
		}
		
		public function show(displayObject:DisplayObject, isCenter:Boolean, isBackground:Boolean):void
		{
			if(isBackground) drawBackground();
			displayObject.x=(_stage.stageWidth-displayObject.width)/2;
			displayObject.y=(_stage.stageHeight-displayObject.height)/2;
			displayObject.addEventListener(Event.REMOVED, removeBackground);
			_stage.addChild(displayObject);
		}
		
		private function drawBackground():void
		{
			if(!_background)
			{
				_background=new Sprite();
				_background.graphics.beginFill(0x000000, 0.5);
				_background.graphics.drawRect(0, 0, _stage.stageWidth, _stage.stageHeight);
				_background.addEventListener(MouseEvent.CLICK, onClick);
				_stage.addChild(_background);
			}
		}
		
		private function onClick(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
		}
		
		private function removeBackground(evt:Event):void
		{
			if(_background)
			{
				if(_background.parent) _background.parent.removeChild(_background);
				_background.removeEventListener(MouseEvent.CLICK, onClick);
			}
			_background=null;
		}
		
		public function register(stage:Stage):void
		{
			_stage=stage;
		}
		
		private static var _instance:DialogBoxManager;
		public static function get Instance() : DialogBoxManager
		{
			if(_instance == null)
			{
				_instance = new DialogBoxManager();
			}
			return _instance;
		}
		
		public function dispose():void
		{
			if(_background)
			{
				_background.removeEventListener(MouseEvent.CLICK, onClick);
				if(_background.parent) _background.parent.removeChild(_background);
			}
			_background=null;
		}
	}
}